/**
 * el estado de un proyecto o tarea sera almacenado como un entero en la base de datos
 * 0: corresponde a false (inactivo)
 * 1: corresponde a true (Activo)
 */
package com.digitalgeko.hourreporter.database;

import android.content.*;
import android.database.*;
import android.database.sqlite.*;

import com.digitalgeko.hourreporter.objects.*;

import java.util.*;

public final class ProyectTaskTableManager {
	
	public static Integer userId;
	
	public static boolean createProyect(Context context, ProyectTaskObject register){
		boolean result = false;
		DataBaseManager manager = new DataBaseManager(context);
		SQLiteDatabase db = manager.getWritableDatabase();
		
		ContentValues cv = new ContentValues();
		cv.put(DataBaseManager.PROY_TASK_FIELD_DESCRIPTION, register.getDescription());
		cv.put(DataBaseManager.PROY_TASK_FIELD_FATHER, register.getFatherId());
		cv.put(DataBaseManager.PROY_TASK_FIELD_NAME, register.getName());
		cv.put(DataBaseManager.PROY_TASK_FIELD_STATUS, register.getStatus());
		cv.put(DataBaseManager.PROY_TASK_FIELD_USER, userId);
		
		Long id = db.insert(DataBaseManager.PROY_TASK_TABLE, null, cv);
		
		if(id != -1){
			register.setUserId(userId);
			register.setId(id.intValue());
			result = true;
		}
		
		db.close();
		manager.close();
		
		return result;
	}
	
	public static List<ProyectTaskObject> findAllProyects(Context context){
		List<ProyectTaskObject> result = new ArrayList<ProyectTaskObject>();
		DataBaseManager manager = new DataBaseManager(context);
		SQLiteDatabase db = manager.getWritableDatabase();
		
		Cursor cursor = db.query(DataBaseManager.PROY_TASK_TABLE, 
				new String[]{DataBaseManager.PROY_TASK_FIELD_ID, DataBaseManager.PROY_TASK_FIELD_NAME, 
				DataBaseManager.PROY_TASK_FIELD_DESCRIPTION, DataBaseManager.PROY_TASK_FIELD_STATUS}, 
				DataBaseManager.PROY_TASK_FIELD_FATHER + " IS NULL and " + DataBaseManager.PROY_TASK_FIELD_USER + "=?", 
				new String[]{Integer.toString(userId)}, null, null, null, null);
		
		if(cursor.moveToFirst()){
			do{
				ProyectTaskObject temp = new ProyectTaskObject(cursor.getInt(0), cursor.getString(1), 
						cursor.getString(2), null, userId);
				temp.setStatus(cursor.getInt(3));
				temp.setChilds(findAllTask(context, temp.getId()));
				result.add(temp);
			}while(cursor.moveToNext());
		}
		
		cursor.close();
		
		db.close();
		manager.close();
		return result;
	}
	
	public static List<ProyectTaskObject> findActiveProyects(Context context){
		List<ProyectTaskObject> result = new ArrayList<ProyectTaskObject>();
		DataBaseManager manager = new DataBaseManager(context);
		SQLiteDatabase db = manager.getWritableDatabase();
		
		Cursor cursor = db.query(DataBaseManager.PROY_TASK_TABLE, 
				new String[]{DataBaseManager.PROY_TASK_FIELD_ID, DataBaseManager.PROY_TASK_FIELD_NAME, DataBaseManager.PROY_TASK_FIELD_DESCRIPTION}, 
				DataBaseManager.PROY_TASK_FIELD_FATHER + " IS NULL and " + DataBaseManager.PROY_TASK_FIELD_USER + "=? and " +
				DataBaseManager.PROY_TASK_FIELD_STATUS + "=?", 
				new String[]{Integer.toString(userId), "1"}, null, null, null);
		
		if(cursor.moveToFirst()){
			do{
				ProyectTaskObject temp = new ProyectTaskObject(cursor.getInt(0), cursor.getString(1), 
						cursor.getString(2), null, userId);
				temp.setChilds(findAllTask(context, temp.getId()));
				result.add(temp);
			}while(cursor.moveToNext());
		}
		
		cursor.close();
		
		db.close();
		manager.close();
		return result;
	}
	
	private static List<ProyectTaskObject> findAllTask(Context context, Integer fatherId){
		List<ProyectTaskObject> result = new ArrayList<ProyectTaskObject>();
		DataBaseManager manager = new DataBaseManager(context);
		SQLiteDatabase db = manager.getWritableDatabase();
		
		Cursor cursor = db.query(DataBaseManager.PROY_TASK_TABLE, 
				new String[]{DataBaseManager.PROY_TASK_FIELD_ID, DataBaseManager.PROY_TASK_FIELD_NAME, DataBaseManager.PROY_TASK_FIELD_DESCRIPTION}, 
				DataBaseManager.PROY_TASK_FIELD_ID + "=? and " + DataBaseManager.PROY_TASK_FIELD_USER + "=?", 
				new String[]{Integer.toString(fatherId), Integer.toString(userId)}, null, null, null);
		
		if(cursor.moveToFirst()){
			do{
				result.add(new ProyectTaskObject(cursor.getInt(0), cursor.getString(1), 
						cursor.getString(2), fatherId, userId));
			}while(cursor.moveToNext());
		}
		
		cursor.close();
		
		db.close();
		manager.close();
		return result;
	}
	
	public static boolean editProyTask(Context context, ProyectTaskObject register){
		boolean result = false;
		DataBaseManager manager = new DataBaseManager(context);
		SQLiteDatabase db = manager.getWritableDatabase();
		
		ContentValues cv = new ContentValues();
		cv.put(DataBaseManager.PROY_TASK_FIELD_DESCRIPTION, register.getDescription());
		cv.put(DataBaseManager.PROY_TASK_FIELD_NAME, register.getName());
		cv.put(DataBaseManager.PROY_TASK_FIELD_STATUS, register.getStatus());
		
		if(db.update(DataBaseManager.PROY_TASK_TABLE, cv, DataBaseManager.PROY_TASK_FIELD_ID + "=?", 
				new String[]{Integer.toString(register.getId())}) > 0){
			result = true;
		}
		
		db.close();
		manager.close();
		return result;
	}

}
